home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / cpp112.zoo / src / utils.c < prev    next >
C/C++ Source or Header  |  1994-07-07  |  5KB  |  200 lines

  1.  
  2. /*---------------------------------------------------------------------*\
  3. |                                    |
  4. | CPP -- a stand-alone C preprocessor                    |
  5. | Copyright (c) 1993 Hacker Ltd.        Author: Scott Bigham    |
  6. |                                    |
  7. | Permission is granted to anyone to use this software for any purpose    |
  8. | on any computer system, and to redistribute it freely, with the    |
  9. | following restrictions:                        |
  10. | - No charge may be made other than reasonable charges for repro-    |
  11. |     duction.                                |
  12. | - Modified versions must be clearly marked as such.            |
  13. | - The author is not responsible for any harmful consequences of    |
  14. |     using this software, even if they result from defects therein.    |
  15. |                                    |
  16. | utils.c -- assorted utility routines                    |
  17. \*---------------------------------------------------------------------*/
  18.  
  19. #include <stdio.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #if defined(__STDC__) || defined(__SOZOBONC__) || defined(__SOZOBONX__)
  23. #include <stdarg.h>
  24. #else
  25. #include <varargs.h>
  26. #endif
  27. #include "global.h"
  28. #include "ztype.h"
  29.  
  30. #define EM_ERROR  0
  31. #define EM_WARN   1
  32. #define EM_FATAL  2
  33. #define EM_BUGCHK 3
  34.  
  35. #ifdef __STDC__
  36. #define __EM_fmt__ (const char *fmt, ...)
  37. #else
  38. # if defined(__SOZOBONC__) || defined(__SOZOBONX__)
  39. # define __EM_fmt__ (fmt) char *fmt;
  40. # else
  41. # define __EM_fmt__ (va_alist) va_dcl
  42. # endif
  43. #endif
  44.  
  45. extern int nerrs;
  46.  
  47. static char *em_text[] =
  48. {"", " warning:", " FATAL:", " compiler bug:"};
  49.  
  50. /* em_header() -- add file and line number information to error message */
  51. static void em_header(n)
  52.   int n;
  53. {
  54.   if (cur_file)
  55.     fprintf(stderr, "In file \"%s\" (%lu):%s ", cur_file, this_line, em_text[n]);
  56.   else
  57.     fprintf(stderr, "%s:%s ", argv0, em_text[n]);
  58. }
  59.  
  60. /* fatal() -- print an error message and punt */
  61. void fatal __EM_fmt__
  62. {
  63.   va_list ap;
  64.     em_header(EM_FATAL);
  65.     va_start(ap, fmt);
  66.     vfprintf(stderr, fmt, ap);
  67.     va_end(ap);
  68.     fputc('\n', stderr);
  69.     exit(1);
  70. }
  71.  
  72. /*
  73.    bugchk() -- similar to fatal(), but reserved for flagging compiler bugs
  74. */
  75. void bugchk __EM_fmt__
  76. {
  77.   va_list ap;
  78.     em_header(EM_BUGCHK);
  79.     va_start(ap, fmt);
  80.     vfprintf(stderr, fmt, ap);
  81.     va_end(ap);
  82.     fputc('\n', stderr);
  83.     exit(1);
  84. }
  85.  
  86. /* error() -- print an error message and bump the error counter */
  87. void error __EM_fmt__
  88. {
  89.   va_list ap;
  90.     em_header(EM_ERROR);
  91.     va_start(ap, fmt);
  92.     vfprintf(stderr, fmt, ap);
  93.     va_end(ap);
  94.     fputc('\n', stderr);
  95.     nerrs++;
  96. }
  97.  
  98. /* warning() -- print an error/informational message */
  99. void warning __EM_fmt__
  100. {
  101.   va_list ap;
  102.     em_header(EM_WARN);
  103.     va_start(ap, fmt);
  104.     vfprintf(stderr, fmt, ap);
  105.     va_end(ap);
  106.     fputc('\n', stderr);
  107. }
  108.  
  109. /* mallok() -- a paranoia wrapper around malloc() */
  110. void *mallok(n)
  111.   size_t n;
  112. {
  113.   register void *t = malloc(n);
  114.  
  115.   if (!t)
  116.     fatal("cannot get %lu bytes", (unsigned long)n);
  117.   return t;
  118. }
  119.  
  120. /* reallok() -- a paranoia wrapper around realloc() */
  121. void *reallok(t, n)
  122.   void *t;
  123.   size_t n;
  124. {
  125.   register void *u = realloc(t, n);
  126.  
  127.   if (!u)
  128.     fatal("cannot get %lu bytes", (unsigned long)n);
  129.   return u;
  130. }
  131.  
  132. /*
  133.    copy_filename() -- return malloc()'ed memory containing the first |len|
  134.    characters of |fnam|, or all of |fnam| if |len==0|
  135. */
  136. char *copy_filename(fnam, len)
  137.   char *fnam;
  138.   int len;
  139. {
  140.   char *newfnam;
  141.   register char *s = fnam, *t;
  142.   register int i;
  143.  
  144.   if (len == 0)
  145.     len = strlen(fnam);
  146.   t = newfnam = mallok(len + 1);
  147.   for (i = 0; i < len; s++, t++, i++)
  148.     *t = ((*s == '/' || *s == '\\') ? PATH_SEP : *s);
  149.   *t = '\0';
  150.   return newfnam;
  151. }
  152.  
  153. #ifndef __GNUC__
  154. /* strdup() -- return malloc()'ed space containing a copy of |s| */
  155. char *strdup(s)
  156.   register const char *s;
  157. {
  158.   return strcpy((char *)mallok(strlen(s) + 1), s);
  159. }
  160.  
  161. #endif
  162.  
  163. /*
  164.    xfopen() -- like fopen(), except that setvbuf() is called to increase the
  165.    buffer size
  166. */
  167. FILE *xfopen(fnam, mode)
  168.   const char *fnam, *mode;
  169. {
  170.   FILE *f = fopen(fnam, mode);
  171.  
  172.   if (!f)
  173.     fatal("cannot open %s file %s", (*mode == 'r' ? "input" : "output"),
  174.       fnam);
  175.   if (setvbuf(f, NULL, _IOFBF, NEWBUFSIZ) != 0)
  176.     fatal("cannot get file buffer for %s", fnam);
  177.   return f;
  178. }
  179.  
  180. /*
  181.    grow() -- expand buffer |*buf|, originally of length |*buflen|, to hold an
  182.    additional |add_len| characters.  |in_ptr| is a pointer into the buffer;
  183.    return a pointer into the new buffer with the same offset.
  184. */
  185. char *grow(buf, buflen, in_ptr, add_len)
  186.   char **buf;
  187.   size_t *buflen;
  188.   char *in_ptr;
  189.   int add_len;
  190. {
  191.   ptrdiff_t dp = in_ptr - *buf;
  192.  
  193.   if (dp + add_len <= *buflen)
  194.     return in_ptr;
  195.   while (*buflen < dp + add_len)
  196.     *buflen *= 2;
  197.   *buf = reallok(*buf, *buflen);
  198.   return *buf + dp;
  199. }
  200.